package gov.nhso.prov.acc.apps.acc6;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;

import com.isud.core.ISUD;
import com.isud.core.ISUDFactory;

import gov.nhso.prov.acc.beans.Acc6FtpBean;
import gov.nhso.prov.acc.core.DBConnection;
import gov.nhso.prov.acc.ftp.FTPUtil;
import gov.nhso.prov.acc.ftp.NHSO2BAAC;
import gov.nhso.prov.acc.utils.Utility;

@SuppressWarnings({"rawtypes","unchecked"})
public class ACC60309 extends Utility{
	public String url = "/apps/acc6/ACC60309A.jsp";

	public ACC60309(HttpServletRequest request, HttpServletResponse response) {
		this.message(request).clearMsg();
		String job = this.getAttr(request, "job");
		if (job.equals("init")) {
			this.init(request);
		} else if (job.equals("search")) {
			this.search(request);
		} else if (job.equals("send")) {
			this.send(request);
		}
	}
	private String init(HttpServletRequest request){
		request.setAttribute("job", "search");
		request.setAttribute("date", this.dateUtil().getThaiCurrentDate());
		return this.url;
	}

	private String search(HttpServletRequest request){
		Connection conn = null;
		ISUD dao = null;
		try {
			conn = DBConnection.getConnection();
			dao = ISUDFactory.create(conn);
			String date = this.getAttr(request, "date");
			String[] strArr = date.split("\\/");
			//String dd = strArr[0];
			String mm = strArr[1];
			String yyyy = String.valueOf(Integer.parseInt(strArr[2])-543);
			Acc6FtpBean afb = new Acc6FtpBean();
			FTPUtil fu = new FTPUtil();
			List<Acc6FtpBean> lsFtp = new ArrayList<Acc6FtpBean>();
			//afb.setCreatedDate(this.dateUtil().changeDateToDB(date));
			lsFtp = dao.selectList(afb , " WHERE TRUNC(CREATED_DATE) = TO_DATE('"+this.dateUtil().changeFormatDate(date,"/","/")+"','DD.MM.YYYY') ");
			//dao.printMessage();
			FTPClient client = new FTPClient();
			client.connect(fu.ftpHost);
			client.login(fu.ftpUserId, fu.ftpPasswd);
			String remoteFolder = fu.ftpNhso2BaacTranfer+"/"+yyyy+"/"+mm+"/";
			FTPFile[] f = client.listFiles(remoteFolder);
			List<String> lsFileName = new ArrayList<String>();
			for (int n = 0; n < f.length; n++) {
				FTPFile ff = f[n];
				if (!ff.getName().equals(".") && !ff.getName().equals("..")) {
					lsFileName.add(ff.getName());
				}
			}
			
			for (Acc6FtpBean bean : lsFtp) {
				if (lsFileName.contains(bean.getFileName())) {
					bean.setStatusFile("F");
				} else {
					bean.setStatusFile("E");
				}
			}
			if (lsFtp.size() <= 0) {
				this.message(request).setMsg("I", "ไม่พบข้อมูล");
			}
			request.setAttribute("lsFtp", lsFtp);
			request.setAttribute("date", date);
			request.setAttribute("job", "search");
			request.setAttribute("mapFTPStatus", this.mapFTPStatus());
			conn.commit();
		} catch (Exception e) {
			try {
				if (conn != null) {
					conn.rollback();
				}
			} catch (SQLException eSQL) {
				eSQL.printStackTrace();
			}
			e.printStackTrace();
			this.message(request).setMsg("E", e.toString() + ":" + dao.getMessage(), e.getStackTrace());
		} finally {
			try {
				if (conn != null) {
					conn.close();
				}
				dao = null;
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return this.url;
	}
	private String send(HttpServletRequest request){
		Connection conn = null;
		ISUD dao = null;
		try {
			conn = DBConnection.getConnection();
			dao = ISUDFactory.create(conn);

			String date = this.getAttr(request, "date");
			NHSO2BAAC n2b = new NHSO2BAAC();
			n2b.sendFTP(date);
			
			conn.commit();
		} catch (Exception e) {
			try {
				if (conn != null) {
					conn.rollback();
				}
			} catch (SQLException eSQL) {
				eSQL.printStackTrace();
			}
			e.printStackTrace();
			this.message(request).setMsg("E", e.toString() + ":" + dao.getMessage(), e.getStackTrace());
		} finally {
			try {
				if (conn != null) {
					conn.close();
				}
				this.search(request);
				dao = null;
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return this.url;
	}
	
	private Map mapFTPStatus(){
		Map map = new HashMap();
		map.put("W", "รอดำเนินการ");
		map.put("Y", "FTP สำเร็จ");
		map.put("N", "FTP ไม่สำเร็จ");
		
		map.put("F", "สร้างบน FTP Server");
		map.put("E", "ไม่มีไฟล์บน FTP Server");
		return map;
	}
}
